// person.js
var app = getApp();
var util = require('../../utils/util.js')
var glo = require('../../utils/global.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        motto: '反馈您的意见:',
        evaContent: '',
        userid: "",
        userInfo: {},
        updateMsg: '',
        notbescount: 0,
        markcounts: 0,
        msgcount: 0,
        header: '',
        nickName: '',
        isNotAdmin: true,
        pageEnable: false,
        aboutShow: false,
        notbetext: "%E7%AC%94%E8%AE%B0",
        dk: decodeURI('%E6%89%93%E5%8D%A1'),
        extraData: {
            id: '41514', //'28898',
            customData: {
                clientInfo: '我家菜谱'
            }
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        this.data.userid = wx.getStorageSync('userid');
        if (glo.validBible()) {
            var extraData0 = {
                id: '35596', //'28898',
                customData: {
                    clientInfo: decodeURI("%E4%B8%BB%E5%86%85") + decodeURI("%E5%9C%A3%E7%BB%8F")
                }
            }
            var bj = decodeURI("%E7%AC%94%E8%AE%B0");
            that.setData({
                extraData: extraData0,
                pageEnable: true,
                notbetext: bj,
                zn: decodeURI("%E4%B8%BB%E5%86%85")
            });

        }

        //调用应用实例的方法获取全局数据
        app.getUserInfo(function(userInfo) {
            //更新数据
            var favorite = userInfo.favorite || '';
            var arr1 = favorite.split(";");
            var arr2 = userInfo.notes;
            var arr3 = userInfo.msgs;
            var admin = userInfo.isAdmin;
            var markscount = 0;
            var notbescount = 0;
            var msgcount = 0;
            if (arr1 != null && arr1 != "" && arr1 != "undefined") {
                markscount = arr1.length - 1;
            }
            if (arr2 != null && arr2 != "" && arr2 != "undefined") {
                notbescount = arr2.length;
            }
            if (arr3 != null && arr3 != "" && arr3 != "undefined") {
                msgcount = arr3.length;
            }
            that.setData({
                userInfo: userInfo,
                markscount: markscount,
                notbescount: notbescount,
                msgcount: msgcount,
                isNotAdmin: !admin
            });

        });


    },
    bindViewTap: function(e) {
        wx.navigateTo({
            url: '../profile/profile',
        })
    },
    tap_about: function(e) {
        if (e.currentTarget.dataset.type === 'show') {
            this.setData({
                aboutShow: true
            });
        } else {
            this.setData({
                aboutShow: false
            });
        }
    },
    tap_practice: function(e) {
        if (e.currentTarget.dataset.type === 'jk') {
            wx.navigateTo({
                url: '../practice/practice?type=1'
            });
        } else {
            wx.navigateTo({
                url: '../practice/practice?type=0'
            });
        }
    },
    tap_daka: function(e) {
        wx.navigateTo({
            url: '../daka/daka',
        })
    },
    tap_notbelist: function(e) {
        wx.navigateTo({
            url: '../notbes/notbes',
        })
    },
    tap_marklist: function(e) {
        wx.navigateTo({
            url: '../marks/marks',
        })
    },

    tap_update: function(e) {
        if (this.data.updateMsg == "") {
            wx.showToast({
                title: '格式已是最新',
            })
            return;
        }
        var that = this;

        wx.getNetworkType({
            success: function(res) {
                // 返回网络类型, 有效值：
                // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
                var networkType = res.networkType
                if (networkType == "wifi") { //wifi 下直接强制更新内容
                    glo.wifiUpdateBibleData(function() {
                        that.setData({
                            updateMsg: ""
                        });
                    });
                    /*
                      wx.request({
                        url: 'https://ssl.zhi22.cn/chapter/list_all',
                        method: 'GET',
                        success: function (res2) {
                          var bibeData = res2.data;
                          for (var k = 0; k < 66; k++) {
                            wx.setStorage({      //添加缓存
                              key: "bk" + (k + 1),
                              data: bibeData[k]
                            });
                          }
                          that.setData({ updateMsg: "" });
                        }
                      })*/

                } else if (networkType == "3g" || networkType == "4g") {
                    wx.showModal({
                        title: '提示',
                        content: '非WIFI下将消耗手机3M流量，是否继续更新',
                        success: function(res) {
                            if (res.confirm) {
                                that.updateBibleData();
                                console.log('用户点击确定')
                            } else if (res.cancel) {
                                console.log('用户点击取消')
                            }
                        }
                    })
                } else {
                    wx.showToast({
                        title: '请检查网络是否连通',
                        icon: 'loading',
                        duration: 2000
                    })
                }
            }
        });
    },

    tapsuggest: function(e) {
        wx.navigateTo({
            url: '../suggest/suggest',
            success: function(res) {
                // success
            }
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    updateBibleData: function() {
        var that = this;
        glo.wifiUpdateBibleData(function() {
            that.setData({
                updateMsg: ""
            });
        });
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        var that = this;
        app.getUserInfo(function(userInfo) { //改
            console.log(userInfo)
            that.setData({
                header: userInfo.avatarUrl,
                nickName: userInfo.nickName
            });

        });
        //获取昵称
        // var nickName = wx.getStorageSync('nickName');//改
        // if (nickName != null && nickName != '') {
        //   that.setData({
        //     nickName: nickName
        //   });
        // } else {
        //   app.getUserInfo(function (userInfo) {
        //     wx.setStorageSync('nickName', userInfo.nickName);
        //     that.setData({
        //       nickName: userInfo.nickName
        //     });

        //   });
        // }


        // //头像
        // wx.getSavedFileList({
        //   success: function (res) {
        //     console.log(res.fileList.length + '1111');


        //     // if (res.fileList.length > 0) {
        //     //   that.setData({
        //     //     header: res.fileList[0].filePath
        //     //   });
        //     // } else {


        //   //  }
        //   }
        // })

        wx.getNetworkType({
            success: function(res) {
                // 返回网络类型, 有效值：
                // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
                var networkType = res.networkType
                if (networkType == "wifi") { //wifi 下直接更新
                    that.updateBibleData();
                } else {
                    wx.request({
                        url: glo.https + '/conf/getval?mkey=version',
                        method: 'GET',
                        success: function(res0) {
                            var ver0 = res0.data;
                            var version = wx.getStorageSync('version')
                            if (version != ver0) {
                                that.setData({
                                    updateMsg: "有更新"
                                });
                            } else {
                                that.setData({
                                    updateMsg: ""
                                });
                            }
                        }
                    });

                }
            }
        });

        /*
            wx.request({
              url: 'https://ssl.zhi22.cn/person/nc',
              method: 'GET',
              success: function (res0) {
                that.setData({ notbescount: res0.data });
              }
            });*/
        /*
        wx.request({
          url: 'https://ssl.zhi22.cn/person/mc',
          method: 'GET',
          success: function (res0) {
            that.setData({ markscount: res0.data });
          }
        });*/
        //调用应用实例的方法获取全局数据
        app.getUserInfo(function(userInfo) {
            //更新数据
            var favorite = userInfo.favorite || '';
            var arr1 = favorite.split(";");
            var arr2 = userInfo.notes;
            that.setData({
                userInfo: userInfo,
                markscount: arr1.length - 1,
                notbescount: arr2.length
            });
        });

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    tap_admin: function(e) {
        wx.navigateTo({
            url: '../admin/admin',
        })
    },

    tap_uploadImage: function(e) {
        let that = this;
        console.log(e);
        wx.chooseImage({
            count: 1,
            sizeType: ['compressed'],
            success: function(res) {
                var tempFilePaths = res.tempFilePaths
                // app.getUserInfo(function (userInfo) {
                //  console.log(JSON.stringify('userInfo=' + userInfo));
                wx.uploadFile({
                    url: glo.https + '/person/savenewposter',
                    filePath: tempFilePaths[0],
                    name: 'file',
                    header: {
                        'content-type': 'multipart/form-data'
                    },
                    method: 'POST',
                    success: function(res) {
                        wx.showToast({
                            title: '上传成功',
                        })
                    },
                    fail: function(err) {
                        wx.showToast({
                            title: '上传失败',
                        })
                    }
                });

                // });
            }
        });

    },

    /**
     * 用户点击右上角
     */
    onShareAppMessage: function() {
        var v = "菜单";
        if (this.data.pageEnable) {
            v = "%E5%9C%A3%E7%BB%8F"
            v = decodeURI(v)
        }
        return {

            title: ' 这是一款免下载的小程序' + v + ',点开即可马上使用！',

            desc: '微信小程序版本' + v,

            path: '/pages/index/index'

        }
    }
})